using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.XPath;
using MagnoliaTableAdapters;

public partial class LiveControl : System.Web.UI.Page
{
    static int LiveControlPackageID;
    static int WebModeTimeoutCount = 0;
    static int RibbonTimeoutCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

        Page.Title = ConfigurationManager.AppSettings["PageTitle"] + " - Live Control";
        Hidden_SessionID.Value = Convert.ToString(Session["SessionID"]);
        Hidden_UserID.Value = Convert.ToString(Session["UserID"]);
        Hidden_EditorPageID.Value = "liveControl";
        Lbl_Header.Text = "Live Control Web Remote";
        //TextArea_7thRibbon.Style.Add("height", "40px");
        //TextArea_7thRibbon.Style.Add("width", "750px");
        TextArea_7thRibbon.Style.Add("overflow", "auto");
        //TextArea_CurrentText.Style.Add("height", "40px");
        //TextArea_CurrentText.Style.Add("width", "750px");
        TextArea_CurrentText.Style.Add("overflow", "auto");

        // call first tick
        EventArgs args = new EventArgs();
        this.Timer_Status_Tick(this, args);

        if (Hidden_LiveControlPackageID.Value.Length < 1)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SuperSignConnectionString"].ConnectionString);
            try
            {
                conn.Open();
                String sql = "SELECT ID FROM PACKAGE WHERE IsWebControlPackage = 1";
                SqlCommand cmd = new SqlCommand(sql, conn);
                LiveControlPackageID = Convert.ToInt32(cmd.ExecuteScalar());
                Hidden_LiveControlPackageID.Value = Convert.ToString(LiveControlPackageID);
                //conn.Close();
            }
            catch (Exception ex)
            {
                String err = ex.Message;
            }
            finally
            {
                conn.Close();
            }
        }
    }
    protected void Timer_Status_Tick(object sender, EventArgs e)
    {
        if (Btn_WebControl.Enabled == false)
        {
            WebModeTimeoutCount++;
            if (WebModeTimeoutCount >= 10)
            {
                Btn_WebControl.Enabled = true;
                WebModeTimeoutCount = 0;
            }
        }

        if (Btn_7thRibbonOverride.Enabled == false)
        {
            RibbonTimeoutCount++;
            if (RibbonTimeoutCount >= 10)
            {
                Btn_7thRibbonOverride.Enabled = true;
                RibbonTimeoutCount = 0;
            }
        }

        // query to get status for web remote live control
        DataTable dtStatus = new DataTable();
        SqlDataAdapter sqlAdapter = new SqlDataAdapter();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SuperSignConnectionString"].ConnectionString);
        try
        {
            String sProc = "sproc_WC_GetStatusForWeb";
            sqlAdapter.SelectCommand = new SqlCommand(sProc, conn);
            sqlAdapter.SelectCommand.Connection.Open();
            sqlAdapter.Fill(dtStatus);
            sqlAdapter.SelectCommand.Connection.Close();

            // fill in labels with results and make adjustments to page
            foreach (DataRow row in dtStatus.Rows)
            {
                if (Convert.ToString(row["Property"]) == "GetStatus")
                {
                    String newStatus = Convert.ToString(row["Value"]);
                    if (newStatus != Lbl_Status.Text)
                    {
                        Btn_WebControl.Enabled = true;
                    }
                    Lbl_Status.Text = newStatus;
                }
                else if (Convert.ToString(row["Property"]) == "Get7thRibbonStatus")
                {
                    String newRibbonStatus = Convert.ToString(row["Value"]);
                    if (newRibbonStatus != Lbl_7thRibbonStatus.Text)
                    {
                        Btn_7thRibbonOverride.Enabled = true;
                    }
                    Lbl_7thRibbonStatus.Text = newRibbonStatus;
                }
                else if (Convert.ToString(row["Property"]) == "GetTimeRemaining")
                {
                    Lbl_TimeRemaining.Text = Convert.ToString(row["Value"]);
                }
                else if (Convert.ToString(row["Property"]) == "GetLastUpdated")
                {
                    Lbl_LastUpdated.Text = "&nbsp;as of&nbsp;" + Convert.ToString(row["Value"]);
                }
                else if (Convert.ToString(row["Property"]) == "GetCurrentString")
                {
                    TextArea_CurrentText.Text = Convert.ToString(row["Value"]);
                }
            }
        }
        catch (Exception ex)
        {
            String err = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }

    protected void Btn_WebControl_Click(object sender, EventArgs e)
    {
        // first save event list to page
        // this is done by OnClientClick

        // then make call to switch
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SuperSignConnectionString"].ConnectionString);
        try
        {
            conn.Open();
            Btn_WebControl.Enabled = false;
            WebModeTimeoutCount = 0;

            String sProc = "sproc_WC_InitiatePackage";
            SqlCommand cmd = new SqlCommand(sProc, conn);
            cmd.CommandType = CommandType.StoredProcedure;

            // set paramter
            DbParameter param = cmd.CreateParameter();
            param.ParameterName = "@PackageID";
            param.Value = LiveControlPackageID;
            param.DbType = DbType.Int32;
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            String err = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }

    protected void Btn_WebControlCancel_Click(object sender, EventArgs e)
    {
        // then make call to cancel web control
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SuperSignConnectionString"].ConnectionString);
        try
        {
            conn.Open();
            String sProc = "sproc_WC_SetReturnSched";
            SqlCommand cmd = new SqlCommand(sProc, conn);
            cmd.CommandType = CommandType.StoredProcedure;


            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            String err = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }

    protected void Btn_ResetOneHour_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SuperSignConnectionString"].ConnectionString);
        try
        {
            conn.Open();
            String sProc = "sproc_WC_SetTimeToMax";
            SqlCommand cmd = new SqlCommand(sProc, conn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            String err = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }

    //protected void Btn_Save_Click(object sender, EventArgs e)
    //{
    //    //try
    //    //{
    //    //    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SuperSignConnectionString"].ConnectionString);
    //    //    String sProc = "sproc_WEB_SaveLiveControlList";
    //    //    SqlCommand cmd = new SqlCommand(sProc, conn);
    //    //    cmd.CommandType = CommandType.StoredProcedure;

    //    //    // set paramter
    //    //    DbParameter param = cmd.CreateParameter();
    //    //    param.ParameterName = "@XML";
    //    //    param.Value = Session["LiveControlSave"];
    //    //    param.DbType = DbType.Xml;
    //    //    cmd.Parameters.Add(param);

    //    //    int Result = Convert.ToInt32(cmd.ExecuteScalar());

    //    //    if (Result == 1)
    //    //    {
    //    //        Lbl_Messages.Text = "Live Event List Saved";
    //    //        Lbl_Messages.Style.Add("color", "green");
    //    //        Lbl_Messages.Visible = true;
    //    //    }
    //    //}
    //    //catch (Exception ex)
    //    //{
    //    //    String err = ex.Message;
    //    //}
    //}

    protected void Btn_7thRibbonOverride_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SuperSignConnectionString"].ConnectionString);
        try
        {
            conn.Open();
            Btn_7thRibbonOverride.Enabled = false;
            RibbonTimeoutCount = 0;

            String sProc = "sproc_WC_SetUpdate7thRibbon";
            SqlCommand cmd = new SqlCommand(sProc, conn);
            cmd.CommandType = CommandType.StoredProcedure;

            // set paramter
            DbParameter param = cmd.CreateParameter();
            param.ParameterName = "@Text";
            param.Value = TextArea_7thRibbon.Text;
            param.DbType = DbType.String;
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            String err = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }

    protected void Btn_7thRibbonOverrideCancel_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SuperSignConnectionString"].ConnectionString);
        try
        {
            conn.Open();
            String sProc = "sproc_WC_SetCancel7thRibbon";
            SqlCommand cmd = new SqlCommand(sProc, conn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            String err = ex.Message;
        }
        finally 
        { 
            conn.Close(); 
        }
    }
}